<?php

namespace app\common\model;

use think\Model;

class ConfigModel extends Model
{
    protected $pk = 'id';
    protected $table = 'basic_config';
    protected $auto = ['config_value'];
    protected $autoWriteTimestamp = true;

    //获取商品列表
    public function getConfigList($params)
    {
        $curPage = 1;
        $pageSize = 15;
        $where['status'] = ['neq', 111];
        if (!empty($params)) {
            checkString($params['config_key']) && $where['config_key'] = ['=', $params['config_key']];
        }

        $totalRow = self::where($where)->count();
        $data = self::where($where)->select();
        $data = empty($data) ? [] : collection($data)->toArray();

        return [
            'pageSize' => $pageSize,
            'totalRow' => $totalRow,
            'curPage' => $curPage,
            'data' => $data
        ];
    }

    public function setConfigValueAttr($value){
        switch ($this->data['config_type']) {
            case 'text':
                return $this->data['text'];
                break;
            case 'image':
                return $this->data['image'];
                break;
            case 'editor':
                return $this->data['editor'];
                break;

            default:
                return '';
                break;
        }
    }

    //获取单个配置项
    public function getConfigById($id){
        return self::find($id);
    }

    public function getConfigBykey($configKey){
        $config = self::where("config_key", $configKey)->find();
        return isset($config['config_value']) ? $config['config_value']: "";
    }

    //删除
    public function deleteall($id)
    {
        if (!is_array($id)) {
            $id = [$id];
        }

        return self::where("id", "in", $id)->update([
            'status' => 111,
            'update_time' => time()
        ]);
    }

}